package com.note.simpleadmin.module.shop.service;

import com.note.simpleadmin.dto.OrderDto;
import com.note.simpleadmin.entity.TbGoods;
import com.note.simpleadmin.entity.TbOrder;
import com.note.simpleadmin.enu.DeleteEnum;
import com.note.simpleadmin.enu.OrderStatusEnum;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.shop.dao.OrderRepository;
import com.note.simpleadmin.module.shop.vo.GoodsVo;
import com.note.simpleadmin.utils.DateUtil;
import com.note.simpleadmin.vo.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 *@ClaseName:OrderService
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/2/23 0023 12:28
 */
@Service
public class OrderService {
    @Autowired
    private OrderRepository orderRepository;
    /*
     * 获取分页信息
     *
     */
    public PageBean findByPage(OrderDto dto) {
        if(dto.getPageNumber()==null) {
            dto.setPageNumber(0);
        }
        if(dto.getLimit()==null) {
            dto.setLimit(20);
        }
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        PageRequest pageRequest = new PageRequest(dto.getPageNumber(),dto.getLimit(), sort);
        Specification<TbOrder> specification = new Specification<TbOrder>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Predicate toPredicate(Root<TbOrder> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(dto.getPhone())) {
                    predicates.add(criteriaBuilder.equal(root.get("phone"),dto.getPhone()));
                }
                if (StringUtils.isNotBlank(dto.getNickName())) {
                    predicates.add(criteriaBuilder.equal(root.get("nickName"),dto.getNickName()));
                }
                if (StringUtils.isNotBlank(dto.getPayType())) {
                    predicates.add(criteriaBuilder.equal(root.get("payType"),dto.getPayType()));
                }
                if (dto.getOrderStatus()!=null) {
                    predicates.add(criteriaBuilder.equal(root.get("orderStatus"),dto.getOrderStatus()));
                }
                if(dto.getStartTime()!=null)
                {
                    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("payTime").as(String.class), DateUtil.getDate(dto.getStartTime())));
                }
                if(dto.getEndTime()!=null)
                {
                    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("payTime").as(String.class), DateUtil.getDate(dto.getEndTime())));
                }
                predicates.add(criteriaBuilder.equal(root.get("isDelete"), DeleteEnum.NORMAL.getValue()));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page<TbOrder> page=orderRepository.findAll(specification,pageRequest);
        PageBean<TbOrder> pageBean=new PageBean(page.getTotalPages(),page.getTotalElements(),page.isFirst(),page.isEmpty(),page.getNumber(),page.getContent());
        return pageBean;
    }
    public TbOrder getOne(String id) throws  DataNotExistException{
        TbOrder order=orderRepository.findByIdAndIsDelete(id,DeleteEnum.NORMAL.getValue());
        if(order==null) {
            throw new DataNotExistException("order not exist");
        }
        return order;
    }
    public void writeWaybillN(String id,String logisticsCode,String logisticsNo) throws  DataNotExistException{
        TbOrder order=this.getOne(id);
        order.setLogisticsCode(logisticsCode);
        order.setLogisticsNo(logisticsNo);
        order.setOrderStatus(OrderStatusEnum.DELIVERED.getValue());
        orderRepository.save(order);
    }
}